package src.domain;

import java.io.Serializable;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Version;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import src.common.MessageStatus;


@Entity
public class MailBox implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Long id;
	private int version;
	private String name;
	private String pass;
	private boolean active;
	private User user;
	private Set<IncomingMessage> incomingMessages;
	private Set<PostOffice> postOffices;

	public MailBox() {
	}

	/*public MailBox(MailBoxDTO mailBoxDTO) {
		this.id = mailBoxDTO.getId();
		this.version = mailBoxDTO.getVersion();
		this.name = mailBoxDTO.getName();
		this.pass = mailBoxDTO.getPass();
		this.user = new User(mailBoxDTO.getUser());
		
		if (mailBoxDTO.getIncomingMessages() != null) {
			Set<IncomingMessage> incomingMessages = new HashSet<IncomingMessage>();
			for (IncomingMessageDTO incomingMessageDTO : mailBoxDTO
					.getIncomingMessages())
				incomingMessages.add(new IncomingMessage(incomingMessageDTO));
			this.incomingMessages = incomingMessages;
		}

		if (mailBoxDTO.getPostOffices() != null) {
			Set<PostOffice> postOffices = new HashSet<PostOffice>();
			for (PostOfficeDTO postOfficeDTO : mailBoxDTO.getPostOffices())
				postOffices.add(new PostOffice(postOfficeDTO));
			this.postOffices = postOffices;
		}
	}*/
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Version
	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	@Column(length = 50,unique=true)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(length = 50)
	public String getPass() {
		return pass;
	}

	public void setPass(String pass) {
		this.pass = pass;
	}
	
	@Column
	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	@ManyToOne
	@Fetch(FetchMode.JOIN)
	@JoinColumn(name = "idUser")
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@OneToMany
	@Cascade(CascadeType.ALL)
	@JoinColumn(name = "idMailBox",insertable=true)
	@Fetch(FetchMode.JOIN)
	public Set<IncomingMessage> getIncomingMessages() {
		return incomingMessages;
	}

	public void setIncomingMessages(Set<IncomingMessage> incomingMessages) {
		this.incomingMessages = incomingMessages;
	}

	
	@ManyToMany
	@Fetch(FetchMode.JOIN)
	@JoinTable(name = "MailBox_PostOffice", joinColumns = @JoinColumn(name = "idMailBox"), inverseJoinColumns = @JoinColumn(name = "idPostOffice"))
	public Set<PostOffice> getPostOffices() {
		return postOffices;
	}

	public void setPostOffices(Set<PostOffice> postOffices) {
		this.postOffices = postOffices;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		MailBox other = (MailBox) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	public void sendMessage(OutgoingMessage outgoingMessage) {

		for (MailBox mailBox : outgoingMessage.getTargetMailboxes()) {

			IncomingMessage incomingMessage = new IncomingMessage();
			incomingMessage.setOutgoingMessage(outgoingMessage);
			incomingMessage.setMessageStatus(MessageStatus.NOT_READ);

			mailBox.getIncomingMessages().add(incomingMessage);
		

		}

		

	}
	
	public String toString(){
		return this.name;
	}
	
	public void addIncomingMessage(IncomingMessage incomingMessage){
		this.incomingMessages.add(incomingMessage);
	}
	
	public void removeIncomingMessage(IncomingMessage incomingMessage){
		this.incomingMessages.remove(incomingMessage);
	}

}
